﻿//********************************************************************************//
//创建日期：
//创建作者：<朱大鹏>
//功能说明：分户水表分配
//last modify:guodengjie 2016-09-20
//********************************************************************************//
using System;
using Web.Class;
using Entity;
using BLL.Access;
using BLL;
using System.Data;

namespace Web.Pages.YongShui._13.LAIHANGL
{
    public partial class FenHuSBFP : BasePage
    {
        #region 属性

        public DataTable DtDataSocure1
        {
            get
            {
                if (Session["DtDataSocure1"] == null)
                {
                    Session["DtDataSocure1"] = new DataTable();
                }
                return Session["DtDataSocure1"] as DataTable;
            }
            set { Session["DtDataSocure1"] = value; }
        }
        public DataTable DtDataSocure2
        {
            get
            {
                if (Session["DtDataSocure2"] == null)
                {
                    Session["DtDataSocure2"] = new DataTable();
                }
                return Session["DtDataSocure2"] as DataTable;
            }
            set { Session["DtDataSocure2"] = value; }
        }
        public DataTable DtDataSocure3
        {
            get
            {
                if (Session["DtDataSocure3"] == null)
                {
                    Session["DtDataSocure3"] = new DataTable();
                }
                return Session["DtDataSocure3"] as DataTable;
            }
            set { Session["DtDataSocure3"] = value; }
        }
        private string YuanYongHuH
        {
            get
            {
                return Session["YuanYongHuH"] == null ? null : Session["YuanYongHuH"].ToString();
            }
            set { Session["YuanYongHuH"] = value; }
        }
        #endregion

        #region 变量
        YS_CB_SHUIBIAOXXBLL BllShuiBiaoXX = new YS_CB_SHUIBIAOXXBLL();

        YS_LH_YONGHUSBGXProcess BllYongHuSBGX = new YS_LH_YONGHUSBGXProcess();

        YS_CB_YONGHUSBGXBLL _YS_CB_YONGHUSBGXBLL = new YS_CB_YONGHUSBGXBLL();

        SpHelper _sphelper = new SpHelper();

        #endregion

        #region 事件

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                InitControl();
                SetBtnStatus();
            }
        }
        protected void BtnFenHu12_Click(object sender, EventArgs e)
        {
            if (DtDataSocure1 != null && DtDataSocure1.Rows.Count > 0)
            {
                if (this.gvFenHu1.FocusedRowIndex == -1)
                {
                    return;
                }
                object shuibiaobh = this.gvFenHu1.GetRowValues(this.gvFenHu1.FocusedRowIndex, "S_SHUIBIAOBH");
                //无数据时克隆原列表结构.
                if (this.DtDataSocure2.Rows.Count == 0)
                {
                    this.DtDataSocure2 = this.DtDataSocure1.Clone();
                }
                //添加到另一户
                foreach (DataRow dr in DtDataSocure1.Rows)
                {

                    if (shuibiaobh.ToString() == dr["S_SHUIBIAOBH"].ToString())
                    {
                        DataRow newdr = this.DtDataSocure2.NewRow();
                        foreach (DataColumn d in DtDataSocure1.Columns)
                        {
                            newdr[d.ColumnName] = dr[d.ColumnName];
                        }
                        this.DtDataSocure2.Rows.Add(newdr);
                    }
                }
                //从原户移除
                for (int i = 0; i < this.DtDataSocure1.Rows.Count; i++)
                {
                    if (shuibiaobh.ToString() == this.DtDataSocure1.Rows[i]["S_SHUIBIAOBH"].ToString())
                    {
                        this.DtDataSocure1.Rows.RemoveAt(i);
                        break;
                    }
                }
                BindDt();

            }
        }

        protected void BtnFenHu13_Click(object sender, EventArgs e)
        {
            if (DtDataSocure1 != null && DtDataSocure1.Rows.Count > 0)
            {

                DataTable dt1 = DtDataSocure1.Clone();
                DataTable dt2 = DtDataSocure1.Clone();
                object id = this.gvFenHu1.GetRowValues(this.gvFenHu1.FocusedRowIndex, "ID");
                if (id != null && Convert.ToInt32(id) > 0)
                {
                    foreach (DataRow dr in DtDataSocure1.Rows)
                    {

                        if (id.ToString() == dr["ID"].ToString())
                        {
                            DataRow newdr = dt2.NewRow();
                            foreach (DataColumn d in DtDataSocure1.Columns)
                            {
                                newdr[d.ColumnName] = dr[d.ColumnName];
                            }
                            dt2.Rows.Add(newdr);
                        }
                        else
                        {
                            DataRow newdr = dt1.NewRow();
                            foreach (DataColumn d in DtDataSocure1.Columns)
                            {
                                newdr[d.ColumnName] = dr[d.ColumnName];
                            }
                            dt1.Rows.Add(newdr);
                        }
                    }
                    this.DtDataSocure1 = dt1;
                    this.DtDataSocure3 = dt2;
                    BindDt();
                }

            }
        }

        protected void BtnFenHu21_Click(object sender, EventArgs e)
        {
            if (DtDataSocure2 != null && DtDataSocure2.Rows.Count > 0)
            {

                DataTable dt1 = DtDataSocure2.Clone();
                DataTable dt2 = DtDataSocure2.Clone();
                object id = this.gvFenHu2.GetRowValues(this.gvFenHu2.FocusedRowIndex, "ID");
                if (id != null && Convert.ToInt32(id) > 0)
                {
                    foreach (DataRow dr in DtDataSocure2.Rows)
                    {

                        if (id.ToString() == dr["ID"].ToString())
                        {
                            DataRow newdr = dt2.NewRow();
                            foreach (DataColumn d in DtDataSocure1.Columns)
                            {
                                newdr[d.ColumnName] = dr[d.ColumnName];
                            }
                            dt2.Rows.Add(newdr);
                        }
                        else
                        {
                            DataRow newdr = dt1.NewRow();
                            foreach (DataColumn d in DtDataSocure2.Columns)
                            {
                                newdr[d.ColumnName] = dr[d.ColumnName];
                            }
                            dt1.Rows.Add(newdr);
                        }
                    }
                    this.DtDataSocure1 = dt2;
                    this.DtDataSocure2 = dt1;
                    BindDt();
                }


            }
        }

        protected void BtnFenHu23_Click(object sender, EventArgs e)
        {
            if (DtDataSocure2 != null && DtDataSocure2.Rows.Count > 0)
            {

                DataTable dt1 = DtDataSocure2.Clone();
                DataTable dt2 = DtDataSocure2.Clone();
                object id = this.gvFenHu2.GetRowValues(this.gvFenHu2.FocusedRowIndex, "ID");
                if (id != null && Convert.ToInt32(id) > 0)
                {
                    foreach (DataRow dr in DtDataSocure2.Rows)
                    {

                        if (id.ToString() == dr["ID"].ToString())
                        {
                            DataRow newdr = dt2.NewRow();
                            foreach (DataColumn d in DtDataSocure1.Columns)
                            {
                                newdr[d.ColumnName] = dr[d.ColumnName];
                            }
                            dt2.Rows.Add(newdr);
                        }
                        else
                        {
                            DataRow newdr = dt1.NewRow();
                            foreach (DataColumn d in DtDataSocure2.Columns)
                            {
                                newdr[d.ColumnName] = dr[d.ColumnName];
                            }
                            dt1.Rows.Add(newdr);
                        }
                    }
                    this.DtDataSocure3 = dt2;
                    this.DtDataSocure2 = dt1;
                    BindDt();
                }
            }
        }

        protected void BtnFenHu31_Click(object sender, EventArgs e)
        {
            if (DtDataSocure3 != null && DtDataSocure3.Rows.Count > 0)
            {

                DataTable dt1 = DtDataSocure3.Clone();
                DataTable dt2 = DtDataSocure3.Clone();
                object id = this.gvFenHu3.GetRowValues(this.gvFenHu3.FocusedRowIndex, "ID");
                if (id != null && Convert.ToInt32(id) > 0)
                {
                    foreach (DataRow dr in DtDataSocure3.Rows)
                    {

                        if (id.ToString() == dr["ID"].ToString())
                        {
                            DataRow newdr = dt2.NewRow();
                            foreach (DataColumn d in DtDataSocure3.Columns)
                            {
                                newdr[d.ColumnName] = dr[d.ColumnName];
                            }
                            dt2.Rows.Add(newdr);
                        }
                        else
                        {
                            DataRow newdr = dt1.NewRow();
                            foreach (DataColumn d in DtDataSocure3.Columns)
                            {
                                newdr[d.ColumnName] = dr[d.ColumnName];
                            }
                            dt1.Rows.Add(newdr);
                        }
                    }
                    this.DtDataSocure1 = dt2;
                    this.DtDataSocure3 = dt1;
                    BindDt();
                }
            }
        }

        protected void BtnFenHu32_Click(object sender, EventArgs e)
        {
            if (DtDataSocure3 != null && DtDataSocure3.Rows.Count > 0)
            {

                DataTable dt1 = DtDataSocure3.Clone();
                DataTable dt2 = DtDataSocure3.Clone();
                object id = this.gvFenHu3.GetRowValues(this.gvFenHu3.FocusedRowIndex, "ID");
                if (id != null && Convert.ToInt32(id) > 0)
                {
                    foreach (DataRow dr in DtDataSocure3.Rows)
                    {

                        if (id.ToString() == dr["ID"].ToString())
                        {
                            DataRow newdr = dt2.NewRow();
                            foreach (DataColumn d in DtDataSocure3.Columns)
                            {
                                newdr[d.ColumnName] = dr[d.ColumnName];
                            }
                            dt2.Rows.Add(newdr);
                        }
                        else
                        {
                            DataRow newdr = dt1.NewRow();
                            foreach (DataColumn d in DtDataSocure3.Columns)
                            {
                                newdr[d.ColumnName] = dr[d.ColumnName];
                            }
                            dt1.Rows.Add(newdr);
                        }
                    }
                    this.DtDataSocure2 = dt2;
                    this.DtDataSocure3 = dt1;
                    BindDt();
                }
            }
        }
        #endregion

        #region 方法
        //private void SetSessionValue()
        //{
        //    if (Session["DtDataSocure1"] != null)
        //    {
        //        this.DtDataSocure1 = Session["DtDataSocure1"] as DataTable;
        //    }
        //    if (Session["DtDataSocure2"] != null)
        //    {
        //        this.DtDataSocure2 = Session["DtDataSocure2"] as DataTable;
        //    }
        //    if (Session["DtDataSocure3"] != null)
        //    {
        //        this.DtDataSocure3 = Session["DtDataSocure3"] as DataTable;
        //    }
        //}

        private void InitControl()
        {
            if (this.YuanYongHuH != Request["YONGHUH"])
            {
                string where = " S_SHUIBIAOBH in (select S_SHUIBIAOBH from YS_CB_YONGHUSBGX a where a.S_YONGHUBH='" + Request["YONGHUH"] + "' ) and i_leixing=0 and i_jlzt=0";
                DataTable dt = BllShuiBiaoXX.SelectYS_CB_SHUIBIAOXXesDynamic(where, "", null);
                if (dt.Rows.Count == 0)
                {
                    PageTools.AlertMsg(this, "分户用户无相关水表信息", "window.close();");
                    return;
                }
            }
            BindData();
        }
        private void BindData()
        {
            string YongHuH = Request["YongHuH"] == null ? "" : Request["YongHuH"];
            string YongHuH1 = Request["YONGHUH1"] == null ? "" : Request["YongHuH1"];
            string YongHuH2 = Request["YONGHUH2"] == null ? "" : Request["YongHuH2"];
            string YongHuH3 = Request["YONGHUH3"] == null ? "" : Request["YongHuH3"];
            string type = Request["type"];
            if (YongHuH.Trim().Length == 0 || YongHuH1.Trim().Length == 0 || YongHuH2.Trim().Length == 0 || (type == "3" && YongHuH3.Trim().Length == 0))
            {
                PageTools.AlertMsg(this, "参数用户号错误", "window.close();");
            }
            if (type != "3")
            {
                gvFenHu3.Enabled = false;
                BtnFenHu13.Enabled = false;
                BtnFenHu23.Enabled = false;
                BtnFenHu31.Enabled = false;
                BtnFenHu32.Enabled = false;
            }
            string where = "";
            //如果是来函流程
            if (!string.IsNullOrEmpty(this.Request["laihanid"]))
            { 
                YS_LH_LAIHANXX laihanxx=new YS_LH_LAIHANXXProcess().SelectYS_LH_LAIHANXX(int.Parse(this.Request["laihanid"]));
                where = "S_LAIHANBH="+laihanxx.S_LAIHANBH+" and S_YONGHUBH='{0}'";
                this.DtDataSocure1 = this.BllYongHuSBGX.SelectYS_LH_YONGHUSBGXesDynamic(string.Format(where, Request["YONGHUH1"]), null);
                this.DtDataSocure2 = this.BllYongHuSBGX.SelectYS_LH_YONGHUSBGXesDynamic(string.Format(where, Request["YONGHUH2"]), null);
                this.DtDataSocure3 = this.BllYongHuSBGX.SelectYS_LH_YONGHUSBGXesDynamic(string.Format(where, Request["YONGHUH3"]), null);
            }
            else if (this.YuanYongHuH != Request["YONGHUH"])//当前重新打开的原用户号与之前记录的原用户号不一致,重新加载原用户水表
            {
                where = "S_SHUIBIAOBH in (select S_SHUIBIAOBH from YS_CB_YONGHUSBGX a where a.S_YONGHUBH='" + Request["YongHuH"] + "' ) and i_leixing=0 and i_jlzt=0";
                this.DtDataSocure1 = this._YS_CB_YONGHUSBGXBLL.SelectYS_CB_YONGHUSBGXesDynamic(where, "");
                this.DtDataSocure2.Rows.Clear();
                this.DtDataSocure3.Rows.Clear();
                this.YuanYongHuH = Request["YONGHUH"];
            }
            //if ((DtDataSocure1 == null || DtDataSocure1.Rows.Count == 0) && (DtDataSocure2 == null || DtDataSocure2.Rows.Count == 0) && (DtDataSocure3 == null || DtDataSocure3.Rows.Count == 0))
            //{
            //    where = " S_SHUIBIAOBH in (select S_SHUIBIAOBH from YS_CB_YONGHUSBGX a where a.S_YONGHUBH='" + YongHuH + "' ) and i_leixing=0 ";
            //    DataTable dt = BllShuiBiaoXX.SelectYS_CB_SHUIBIAOXXesDynamic(where, "", null);
            //    this.DtDataSocure1 = dt;
            //}
            //SetSessionValue();
            BindDt();
        }
        public string GetShuiBiaoMC(string shuibiaobh)
        {
            string name = "";

            string where = "S_SHUIBIAOBH='" + shuibiaobh + "'";
            DataTable dt = BllShuiBiaoXX.SelectYS_CB_SHUIBIAOXXesDynamic(where, "", null);
            if (dt != null && dt.Rows.Count > 0)
            {
                name = dt.Rows[0]["S_MINGCHENG"].ToString();
            }

            return name;
        }
        private void BindDt()
        {

            gvFenHu1.DataSource = this.DtDataSocure1;
            gvFenHu1.DataBind();

            gvFenHu2.DataSource = this.DtDataSocure2;
            gvFenHu2.DataBind();

            gvFenHu3.DataSource = this.DtDataSocure3;
            gvFenHu3.DataBind();

            //Session["DtDataSocure1"] = DtDataSocure1;
            //Session["DtDataSocure2"] = DtDataSocure2;
            //Session["DtDataSocure3"] = DtDataSocure3;
        }

        private void SetBtnStatus()
        {
            bool b = Request["sl"] != "1";

            BtnFenHu12.Visible = b;
            BtnFenHu13.Visible = b;

            BtnFenHu21.Visible = b;
            BtnFenHu23.Visible = b;

            BtnFenHu31.Visible = b;
            BtnFenHu32.Visible = b;
        }
        #endregion

    }
}